package cn.xopencode.recordservice.service.drytransformer;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.xopencode.common.framework.exception.util.ServiceExceptionUtil;
import cn.xopencode.common.framework.util.DateUtil;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.recordservice.convert.drytransformer.DryTransformerRecordConvert;
import cn.xopencode.recordservice.convert.drytransformer.DryTransformerRecordEmpRelConvert;
import cn.xopencode.recordservice.convert.robotroom.AdminRecordConvert;
import cn.xopencode.recordservice.dal.mysql.dataobject.drytransformer.KvDryTransformerRecordDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.drytransformer.KvDryTransformerRecordEmpRelDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.pdroom.PowerDistributionRoomDO;
import cn.xopencode.recordservice.dal.mysql.mapper.drytransformer.DryTransformerRecordEmpRelMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.drytransformer.DryTransformerRecordMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.pdroom.PowerDistributionRoomMapper;
import cn.xopencode.recordservice.rpc.drytransformer.vo.DryTransformerRecordAdminPageVO;
import cn.xopencode.recordservice.rpc.drytransformer.vo.DryTransformerRecordPageVO;
import cn.xopencode.recordservice.rpc.drytransformer.vo.DryTransformerRecordUpdateVO;
import cn.xopencode.recordservice.service.drytransformer.bo.DryTransformerRecordCreateBO;
import cn.xopencode.recordservice.service.drytransformer.bo.DryTransformerRecordPageBO;
import cn.xopencode.recordservice.service.drytransformer.bo.DryTransformerRecordUpdateBO;
import cn.xopencode.recordservice.service.robotroom.bo.AdminRecordPageBO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.xopencode.recordservice.enums.RecordErrorCodeConstants.RECORD_DRY_TRANSFORMER_NOT_EXITS;
import static cn.xopencode.recordservice.enums.RecordErrorCodeConstants.RRECORD_DRY_TRANSFORMER_NOT_EXCEPTION;

/**
 * 10KV干式变压器巡检记录 Service
 */
@Service
@Validated
public class DryTransformerRecordService {

    @Autowired
    private DryTransformerRecordEmpRelMapper dryTransformerRecordEmpRelMapper;

    @Autowired
    private DryTransformerRecordMapper dryTransformerRecordMapper;

    @Autowired
    private PowerDistributionRoomMapper powerDistributionRoomMapper;

    public Integer createDryTransformerRecord(DryTransformerRecordCreateBO createBO, Set<String> empIds){
        KvDryTransformerRecordDO recordDO = DryTransformerRecordConvert.INSTANCE.convert(createBO);
        String roomId = IdUtil.objectId();
        recordDO.setId(roomId);
        Integer result = dryTransformerRecordMapper.insert(recordDO);
        //保存关联的班组信息
        if (!CollectionUtil.isEmpty(empIds)) {
            List<KvDryTransformerRecordEmpRelDO> dryTransformerRecordEmpRelDOList = empIds.stream()
                    .map(empId -> new KvDryTransformerRecordEmpRelDO().setId(IdUtil.objectId()).setRecordId(roomId).setEmpId(empId)).collect(Collectors.toList());
            dryTransformerRecordEmpRelMapper.insertList(dryTransformerRecordEmpRelDOList);
        }
        return result;
    }

    public PageResult<DryTransformerRecordPageVO> pageDryTransformerRecord(DryTransformerRecordPageBO pageBO) {
        IPage<KvDryTransformerRecordDO> roomDOIPage =  dryTransformerRecordMapper.selectPage(pageBO);
        PageResult<DryTransformerRecordPageVO> boPageResult = DryTransformerRecordConvert.INSTANCE.convertPage(roomDOIPage);
        if(!CollectionUtil.isEmpty(boPageResult.getList())){
            List<DryTransformerRecordPageVO> list = boPageResult.getList();
            for(DryTransformerRecordPageVO roomVO : list){
                List<KvDryTransformerRecordEmpRelDO> empRelDOList = dryTransformerRecordEmpRelMapper.selectListByRecordId(roomVO.getId());
                List<String> tmp = empRelDOList.stream().map(KvDryTransformerRecordEmpRelDO::getEmpId).collect(Collectors.toList());
               roomVO.setEmpIds(String.join(",", tmp));
            }
        }
        return boPageResult;
    }

    public DryTransformerRecordUpdateVO getDryTransformerRecordById(String id){
        KvDryTransformerRecordDO roomDO = dryTransformerRecordMapper.selectById(id);
        if(roomDO != null){
            DryTransformerRecordUpdateVO roomVO = DryTransformerRecordConvert.INSTANCE.convertUpdate(roomDO);
            //查询所有关联的巡检组
            List<KvDryTransformerRecordEmpRelDO> relDOList = dryTransformerRecordEmpRelMapper.selectListByRecordId(id);
            roomVO.setEmpList(DryTransformerRecordEmpRelConvert.INSTANCE.convertList2(relDOList));

            PowerDistributionRoomDO pdRoomDO = powerDistributionRoomMapper.selectById(roomDO.getRoomId());
            if(pdRoomDO != null) {
                roomVO.setRoomName(pdRoomDO.getRoomName());
            }
            return roomVO;
        }
        return null;
    }

    public Integer updateDryTransformerRecord(DryTransformerRecordUpdateBO updateBO, Set<String> empIds){
        KvDryTransformerRecordDO roomDO = DryTransformerRecordConvert.INSTANCE.convert(updateBO);
        Integer result = dryTransformerRecordMapper.updateById(roomDO);

        //先删除所有关联的班组
        dryTransformerRecordEmpRelMapper.deleteByRecordId(roomDO.getId());

        //保存关联的班组信息
        if (!CollectionUtil.isEmpty(empIds)) {
            List<KvDryTransformerRecordEmpRelDO> dryTransformerRecordEmpRelDOList = empIds.stream()
                    .map(empId -> new KvDryTransformerRecordEmpRelDO().setId(IdUtil.objectId()).setRecordId(roomDO.getId()).setEmpId(empId)).collect(Collectors.toList());
           dryTransformerRecordEmpRelMapper.insertList(dryTransformerRecordEmpRelDOList);
        }
        return result;
    }

    public Integer dealDryTranformerRecordException(String recordId){
        KvDryTransformerRecordDO recordDO = dryTransformerRecordMapper.selectById(recordId);
        if(recordDO == null){
            throw ServiceExceptionUtil.exception(RECORD_DRY_TRANSFORMER_NOT_EXITS);
        }
        if(recordDO != null && recordDO.getNoticeLeader() != true){
            throw ServiceExceptionUtil.exception(RRECORD_DRY_TRANSFORMER_NOT_EXCEPTION);
        }
        recordDO.setLeaderDeal(true);
        return dryTransformerRecordMapper.updateById(recordDO);
    }

    public PageResult<DryTransformerRecordAdminPageVO> getAdminPageDryTranformerRecord(AdminRecordPageBO pageBO){
        Page<KvDryTransformerRecordDO> page = new Page<>(pageBO.getPageNo(),pageBO.getPageSize());
        if(pageBO.getInspectionTime() != null){
            String date = DateUtil.format(pageBO.getInspectionTime(), "yyyy-MM-dd");
            pageBO.setInspectionTime1(date.concat(" 00:00:00"));
            pageBO.setInspectionTime2(date.concat(" 23:59:59"));
        }
        IPage<KvDryTransformerRecordDO> recordDOList = dryTransformerRecordMapper.adminQuery(page,pageBO);
        return AdminRecordConvert.INSTANCE.convert4(recordDOList);
    }
}
